package org.kjk.mail.gmail;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.kjk.mail.Mail;
import org.springframework.stereotype.Component;

@Component("gmail")
public class GMail extends Mail{

	public boolean postMail(String recipients[], String subject, String content) {
		boolean debug = false;
		// java.security.Security
		// .addProvider(new com.sun.net.ssl.internal.ssl.Provider());

		String SMTP_HOST_NAME = "gmail-smtp.l.google.com";
		// Properties
		Properties props = new Properties();
		props.put("mail.transport.protocol", "smtp");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", SMTP_HOST_NAME);
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.user", this.getUser());
		props.put("mail.smtp.password", this.pass);
		Session session1 = Session.getDefaultInstance(props,
				new SMTPAuthenticator(getUser(), pass));
		session1.setDebug(debug);

		// create a message
		Message msg = new MimeMessage(session1);
		// set the from and to address
		InternetAddress addressFrom;
		try {
			addressFrom = new InternetAddress(this.getUser());
			msg.setFrom(addressFrom);
			InternetAddress[] addressTo = new InternetAddress[recipients.length];
			for (int i = 0; i < recipients.length; i++) {
				addressTo[i] = new InternetAddress(recipients[i]);
			}
			msg.setRecipients(Message.RecipientType.TO, addressTo);

			// Setting the Subject and Content Type
			msg.setSubject(subject);
			msg.setContent(content, "text/plain;charset=\"utf-8\"");
			Transport.send(msg);
		} catch (AddressException e) {
			e.printStackTrace();
			return false;
		} catch (MessagingException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public boolean sendHanmail(String to[], String subject, String content) {
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", 465);
		props.put("mail.smtp.user", this.getUser());
		props.put("mail.smtp.password", this.pass);
		props.put("mail.smtp.auth", "true");

		Session session1 = Session.getDefaultInstance(props,
				new Authenticator() {
					@Override
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(getUser(), pass);
					}
				});
		Message simpleMessage = new MimeMessage(session1);
		InternetAddress fromAddress = null;
		InternetAddress[] toAddress = null;
		try {
			fromAddress = new InternetAddress(this.getUser());
			toAddress = new InternetAddress[to.length];
			for (int i = 0; i < to.length; i++) {
				toAddress[i] = new InternetAddress(to[i]);
			}
			simpleMessage.setFrom(fromAddress);
			simpleMessage.setRecipients(Message.RecipientType.TO, toAddress);

			simpleMessage.setSubject(subject);
			// simpleMessage.setContent(content,
			// "text/plain;charset=\"utf-8\"");
			simpleMessage.setText(content); // sometimes
			// Transport.send(simpleMessage); is used, but for gmail it's
			// different
			Transport transport = session.getTransport("smtps");
			transport.connect("smtp.gmail.com", 465, getUser(), pass);
			transport.sendMessage(simpleMessage,
					simpleMessage.getAllRecipients());
			transport.close();
		} catch (AddressException e) {
			e.printStackTrace();
			return false;
		} catch (MessagingException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public void readForce() {
		this.receivingHost = "imap.gmail.com";// for imap protocol
		Properties props2 = System.getProperties();
		props2.setProperty("mail.store.protocol", "imaps");
		// I used imaps protocol here
		session = Session.getDefaultInstance(props2, new SMTPAuthenticator(
				this.getUser(), this.pass));
		try {
			store = session.getStore("imaps");
			store.connect(this.receivingHost, this.getUser(), this.pass);
			folder = store.getFolder("INBOX");// get inbox
			folder.open(Folder.READ_ONLY);// open folder only to read
			message = folder.getMessages();
			// for (int i = 0; i < message.length; i++) {
			// System.out.println(message[i].getSubject());
			// System.out.println("\t" + message[i].getContent());
			// }
		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}

}
